﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using OnePiece.Core.Enumeration;


namespace OnePiece.Core.Utility
{
    public class OperationStatus
    {
        /// <summary>
        /// Generic Operation status
        /// </summary>
        /// <param name="status">Enumeration.OperationStatus</param>
        /// <param name="messages">The message to be displayed</param>
        /// <returns>Json Format operation status message</returns>
        public static JsonResult Status(StatusName status, string[] messages)
        {
            var _status = Enum.GetName(typeof(StatusName), status);
            return new JsonResult { Data = new { Status = _status, Messages = messages } };

        }
        /// <summary>
        /// Failed Operation usually for catch statements
        /// </summary>
        /// <param name="ex">The exception that was thrown</param>
        /// <returns>Json Format operation  status message</returns>
        public static JsonResult Status(System.Exception ex)
        {
            var errorMessage = new List<string> {ex.Message};

         
            var innerException = ex.InnerException;
            if (innerException != null)
            {
                errorMessage.Add(ex.InnerException.Message);
                if (ex.InnerException.InnerException != null) errorMessage.Add(ex.InnerException.InnerException.Message);
            }

            return Status(StatusName.Failed, errorMessage.ToArray());
        }

      
        /// <summary>
        /// Generic Operation status
        /// </summary>
        /// <param name="status">Enumeration.OperationStatus</param>
        /// <param name="data">The message to be displayed</param>
        /// <returns>Json Format operation status message</returns>
        public static JsonResult Status(StatusName status, object data)
        {
            var _status = Enum.GetName(typeof(StatusName), status);
            return new JsonResult { Data = new { Status = _status, Messages = data } };

        }

        /// <summary>
        /// Generic Operation status
        /// </summary>
        /// <param name="status">Enumeration.OperationStatus</param>
        /// <param name="data">The message to be displayed</param>
        /// <returns>Json Format operation status message</returns>
        public static JsonResult Status(StatusName status, string data)
        {
            return Status(status, new[] { data });
        }

        /// <summary>
        /// Retrieves the model states errores from data annotations
        /// </summary>        
        /// <param name="modelStateDictionary"></param>
        /// <returns></returns>
        public static JsonResult Status(ModelStateDictionary modelStateDictionary)
        {
            var _status = Enum.GetName(typeof(StatusName), StatusName.Failed );
            var errorMessage = new List<string>();
            foreach (var valuePair in modelStateDictionary)            
                foreach (var invalid in valuePair.Value.Errors)
                    errorMessage.Add(invalid.ErrorMessage);    
            
            
            return new JsonResult { Data = new { Status = _status, Messages = errorMessage.ToArray() } };

        }


    }
}
